package com.zzmltc.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zzmltc.client.DictFeignClient;
import com.zzmltc.hosp.service.HospitalService;
import com.zzmstc.model.hosp.Hospital;
import com.zzmstc.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import com.zzmltc.hosp.repository.HospitalRepository;

import java.util.*;

//让他对外进行调用
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;



//    上传医院接口
    @Override
    public void save(Map<String, Object> paramMap) {
//        先把参数map集合，转化为对象
        String mapString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);
//        第一步判断是否有相同的数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);

//        不存在进行添加
//        存在进行修改
        if(hospitalExist != null) {
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else {
//          0：未上线 1：已上线
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }

    }

//    根据医院方法进行查询
    @Override
    public Hospital getByHocode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }
//    医院列表(条件查询带分页)
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
//        创建pageable对象
        Pageable pageable= PageRequest.of(page-1,limit);
//        条件查询
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
//        hospitalQueryVo转换Hospital对象
        Hospital hospital=new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
//        创建对象
        Example<Hospital> example =Example.of(hospital,matcher);
//        调用方法实现查询
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        List<Hospital> content = pages.getContent();
//        获取LIst集合，遍历进行医院等级的一个封装
        pages.getContent().stream().forEach(item->{
            this.setHospitalHosType(item);
        });
        return pages;
    }
    //更新医院状态
    @Override
    public void updateStatus(String id, Integer status) {
//        根据id查询医院信息
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }
    //    医院详细信息
    @Override
    public Map<String,Object> getByHospById(String id) {
        Map<String,Object> result=new HashMap<>();
        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());
//        医院的基本信息（包含医院的等级）
        result.put("hospital",hospital);
        result.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return result;
    }
//获取医院名称
    @Override
    public String getHospName(String hoscode) {
        Hospital hospitalByHoscode = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospitalByHoscode!=null){
            return hospitalByHoscode.getHosname();
        }
        return null;
    }
    //    根据医院名称查询医院
    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }
    //    根据医院编号获取医院预约挂号详情
    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospitalByHoscode = hospitalRepository.getHospitalByHoscode(hoscode);
        Hospital hospital = this.setHospitalHosType(hospitalByHoscode);
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }


    //获取查询list集合，遍历进行医院等级封装
    private Hospital setHospitalHosType(Hospital hospital) {
//        根据dictCoder和value获取医院等级
        String hostypeString = dictFeignClient.getName("hostype", hospital.getHostype());
//        查询省 市 地区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("hostypeString",hostypeString);

        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);

        return hospital;

    }
}
